package com.yyr.callmessage.core.util;

import com.google.common.base.Charsets;
import com.google.common.base.Strings;

public class StringUtil {

    public static String byteToString(byte[] bytes) {
        int length = 0;
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] == 0) {
                length = i;
                break;
            }
        }
        String result = "";
        try {
            result = new String(bytes, 0, length, Charsets.UTF_8);
        } catch (Exception err) {
            result = "";
        }

        return result;
    }

    /**
     * 字符串做星号处理
     * @param content
     * @return
     */
    public static String getStarString(String content) {
        if (Strings.isNullOrEmpty(content)) {
            return "";
        }

        if (content.length() == 1) {
            return "*";
        }

        // 隐藏一半左右作为*号, 把*号放到前面
        int starCount = (int)Math.ceil(content.length() / 2.0); //向上取整
        String star = Strings.repeat("*", starCount);
        return star + content.substring(starCount);

    }
}
